<template>
	<view class="search">
		<view class="header" v-if="pageType == 1">
			<headerSearch style="width: 100%" ref="search" bg-color="#EEEEEE" @searchBlur="searchBlur"></headerSearch>
			<!-- <button v-if="!isSou" @click="search"></button> -->
			<!-- <view class="btn" @click="search">
				搜索
			</view> -->
		</view>
		<!-- <view class="history" v-if="!isSou">
			<view class="title">
				<text>搜索记录</text>
				<image src="@/static/image/search_del.png" @click="delRlist" mode="widthFix"></image>
			</view>
			<view class="h-list">
				<view class="item" v-for="(item,i) in r_list" :key="i" @click="r_click(item.text)">
					{{item.text}}
				</view>
			</view>
		</view> -->

		<view class="screen">
			<view class="tab">
				<view class="tab-item" :class="tab == 1 ? 'select' : ''" @click="tabClick(1)">
					<text>综合</text>
				</view>
				<view class="tab-item" :class="tab == 2 ? 'select' : ''" @click="tabClick(2, sort)">
					<text>价格</text>
					<image v-if="sort_price == 1" :src="$img_path('/course/sort_1.png')" mode="widthFix"></image>
					<image v-if="sort_price == 2" :src="$img_path('/course/sort_2.png')" mode="widthFix"></image>
				</view>
				<view class="tab-item" :class="tab == 3 ? 'select' : ''" @click="tabClick(3, sort)">
					<text>销量</text>
					<image v-if="sort_num == 1" :src="$img_path('/course/sort_1.png')" mode="widthFix"></image>
					<image v-if="sort_num == 2" :src="$img_path('/course/sort_2.png')" mode="widthFix"></image>
				</view>
				<view class="tab-item box-style" @click="tabClick(4, box_style)">
					<image v-if="box_style == 1" :src="$img_path('/course/s_i_1.png')" mode="widthFix"></image>
					<image v-if="box_style == 2" :src="$img_path('/course/s_i_2.png')" mode="widthFix"></image>
				</view>
			</view>
		</view>
		<view class="p-list">
			<view class="item" :class="box_style == 2 ? 'column-class' : 'row-class'" v-for="(item, i) in goodsList"
				:key="item.id" :style="{ width: box_style == 1 ? '343rpx' : '100%' }">
				<goodsBox :styleType="box_style" :goodsDetail="item" :pageUrl="'/pagesMall/goods/detail?id=' + item.id"
					:imageStyle="{
            width: box_style == 2 ? '260rpx' : '343rpx',
            height: box_style == 2 ? '260rpx' : '343rpx',
          }">
					<text slot="center" style="margin-top: 16rpx; display: block">销量：{{ item.sales }}</text>
					<view slot="tagList" class="tagList" v-if="item.labels">
						<view class="tag-item" v-for="(t_i, idx) in item.labels.split(',')" :key="idx">
							{{ t_i }}
						</view>
					</view>
					<view slot="tagList" class="tagList" v-if="!item.labels"></view>
				</goodsBox>
			</view>
		</view>
		<view class="b-tip">
			{{ contentText[like_status] }}
		</view>
	</view>
</template>

<script>
	import {
		listsNew
	} from "@/utils/api/mallApi.js";
	import headerSearch from "@/components/header-search/header-search.vue";
	import goodsBox from "../../components/service-box/mall-item-box.vue";
	export default {
		components: {
			headerSearch,
			goodsBox,
		},
		data() {
			return {
				navTitle: "",
				pageType: null,
				isSou: false,
				r_list: [],
				name: "",
				sort_price: 1,
				sort_num: 1,
				tab: 1,
				box_style: 1,
				goodsList: [],
				searchValue: "",

				page: 1,
				last_page: 0,
				total: 0,
				contentText: {
					contentdown: "上滑加载更多",
					contentrefresh: "加载中...",
					contentnomore: "没有更多了",
				},
				like_status: "",
				class_id: "",
			};
		},
		onLoad(data) {
			console.log(data);
			this.navTitle = data.title;
			this.pageType = data.type;
			if (data.content) {
				this.searchValue = data.content
				uni.setNavigationBarTitle({
					title: data.content,
				});
			}
			if (data.classify) {
				this.class_id = data.classify;
			}
		},
		mounted() {
			this.$refs.search.inputValue = this.searchValue;
		},
		onShow() {
			this.getRlist();
			this.getGoodsList();
		},
		// 滚动到底部
		onReachBottom(e) {
			if (this.page < this.last_page) {
				this.page++;
				this.getGoodsList();
			}
		},
		methods: {
			getRlist() {
				if (uni.getStorageSync("r_list_mall")) {
					this.r_list = JSON.parse(uni.getStorageSync("r_list_mall"));
				} else {
					this.r_list = [];
				}
			},

			getGoodsList() {
				let obj = {
					category_id: this.class_id,
					keywords: this.searchValue,
					sort: this.tab == 2 ? "price" : this.tab == 3 ? "sales" : "",
					order: this.tab == 2 ?
						this.sort_price == 1 ?
						"desc" :
						"ace" : this.tab == 3 ?
						this.sort_num == 1 ?
						"desc" :
						"ace" : "",
					type: "goods",
					page: this.page,
				};
				this.like_status = "contentrefresh";
				uni.showLoading({
					title: "Loading...",
				});
				listsNew(obj).then((res) => {
					this.last_page = res.data.last_page;
					this.total = res.data.total;
					// console.log(res,'-')
					uni.hideLoading();
					if (this.page == 1) {
						this.goodsList = res.data.data;
					} else {
						this.goodsList = [...res.data.data, ...this.goodsList];
					}

					if (this.page < res.data.last_page) {
						this.like_status = "contentdown";
					} else {
						this.like_status = "contentnomore";
					}
				});
			},
			searchBlur(val) {
				console.log(val,'searchBlursearchBlursearchBlursearchBlur')
				this.searchValue = val.detail.value;
				this.getGoodsList();
			},
			tabClick(type, val) {
				switch (type) {
					case 1:
						break;
					case 2:
						if (this.tab == type) {
							if (this.sort_price == 1) {
								this.sort_price = 2;
							} else {
								this.sort_price = 1;
							}
						}
						break;
					case 3:
						if (this.tab == type) {
							if (this.sort_num == 1) {
								this.sort_num = 2;
							} else {
								this.sort_num = 1;
							}
						}
						break;
					default:
						if (this.box_style == 1) {
							this.box_style = 2;
						} else {
							this.box_style = 1;
						}
						break;
				}
				if (type != 4) {
					this.tab = type;
				}
				this.getGoodsList();
			},

			search() {
				if (
					this.searchValue &&
					this.r_list.filter((e) => {
						return e.text == this.searchValue;
					}).length < 1
				) {
					this.r_list.push({
						text: this.searchValue,
					});
					uni.setStorageSync("r_list_mall", JSON.stringify(this.r_list));
				}

				this.getGoods();
			},
			close() {
				this.name = "";
				// this.getGoods()
				// this.getRlist()
			},
			r_click(text) {
				this.name = text;
				// this.getGoods()
			},
			delRlist() {
				uni.showModal({
					content: "确定删除历史记录？",
					success: (res) => {
						if (res.confirm) {
							uni.removeStorageSync("r_list_mall");
							this.getRlist();
						}
					},
				});
			},
		},
	};
</script>
<style>
	page {
		background-color: #ffffff;
	}
</style>
<style lang="scss" scoped>
	.search {
		box-sizing: border-box;

		.header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			position: relative;
			z-index: 9;
			padding: 20rpx 30rpx 0;
			// background-color: #FFFFFF;

			.btn {
				width: 86rpx;
				font-weight: 400;
				font-size: 28rpx;
				color: #fd845c;
				text-align: right;
			}
		}

		.history {
			margin-top: 40rpx;
			padding: 0 30rpx;

			.title {
				display: flex;
				align-items: center;
				justify-content: space-between;

				text {
					font-family: PingFang SC, PingFang SC;
					font-weight: bold;
					font-size: 30rpx;
					color: #333333;
					line-height: 42rpx;
					text-align: left;
					font-style: normal;
					text-transform: none;
				}

				image {
					width: 40rpx;
					height: 42rpx;
				}
			}

			.h-list {
				width: calc(100% + 15rpx);
				display: flex;
				flex-wrap: wrap;

				.item {
					min-width: 136rpx;
					height: 64rpx;
					margin-right: 17rpx;
					background-color: #f5f5f5;
					border-radius: 30rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #999999;
					line-height: 64rpx;
					text-align: center;
					font-style: normal;
					text-transform: none;
					padding: 0 20rpx;
					box-sizing: border-box;
					margin-top: 40rpx;
				}
			}
		}

		.s-drow {
			position: relative;
		}

		.screen {
			padding: 30rpx 30rpx 0;

			.tab {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 16rpx 24rpx 14rpx;
				// background-color: #FFFFFF;
				border-radius: 10rpx;

				// margin-bottom: 32rpx;
				.tab-item {
					display: flex;
					justify-content: center;
					align-items: center;

					text {
						font-weight: bold;
						font-size: 32rpx;
						color: #999999;
						line-height: 44rpx;
					}

					&.select {
						text {
							color: #e72a2a;
						}
					}

					image {
						width: 30rpx;
						height: 30rpx;
					}
				}
			}
		}

		.p-list {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;
			padding: 20rpx 22rpx 10rpx;

			.item {
				box-sizing: border-box;

				&.column-class {
					padding: 20rpx 0;
					border-bottom: 2rpx solid #f8f8f8;

					&:first-child {
						padding-top: 0;
					}
				}

				&.row-class {
					padding-bottom: 20rpx;
				}

				.tagList {
					display: flex;
					align-items: center;
					margin-top: 16rpx;

					.tag-item {
						// width: 118rpx;
						height: 34rpx;
						border-radius: 6rpx 6rpx 6rpx 6rpx;
						border: 2rpx solid #0291ff;
						font-weight: 400;
						font-size: 20rpx;
						color: #0291ff;
						// line-height: 34rpx;
						text-align: center;
						padding: 3rpx 12rpx;
						box-sizing: border-box;
						margin-left: 12rpx;

						&:first-child {
							background: #ff6602;
							border-color: #ff6602;
							color: #ffffff;
							margin-left: 0;
						}
					}
				}
			}
		}

		.b-tip {
			text-align: center;
			padding: 20rpx 0 54rpx;
			color: #dadada;
			font-size: 28rpx;
		}
	}
</style>